package com.simple.crawler.connect.http.request;

import com.simple.crawler.connect.Request;
import com.simple.crawler.connect.http.HttpProxy;
import com.simple.crawler.constant.HttpMethod;

import java.net.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName HttpRquest
 * @Author Administrator
 * @Desc TODO
 * @CreateTime 2019/8/3 22:07
 */
public class HttpRequest extends Request {

    private HttpMethod method;

    protected HttpProxy proxy;

    protected Map<String, String> header = new HashMap<>();

    protected byte[] body;

    public HttpMethod getMethod() {
        if (method == null) {
            method = HttpMethod.GET;
        }
        return method;
    }

    public void setMethod(HttpMethod method) {
        this.method = method;
    }

    public HttpProxy getProxy() {
        return proxy;
    }

    public void setProxy(HttpProxy proxy) {
        this.proxy = proxy;
    }

    public Map<String, String> getHeader() {
        return header;
    }

    public void addHeader(String key, String value) {
        this.header.put(key, value);
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public static class HttpRequestBuilder {

        private HttpRequest request = new HttpRequest();

        public static HttpRequestBuilder create() {
            return new HttpRequestBuilder();
        }

        public HttpRequestBuilder url(String url) {
            this.request.setUrl(url);
            return this;
        }

        public HttpRequestBuilder proxy(String host, int port) {
            this.request.setProxy(new HttpProxy(host, port));
            return this;
        }

        public HttpRequestBuilder proxy(String host, int port, String userName, String userPassword) {
            this.request.setProxy(new HttpProxy(host, port, userName, userPassword));
            return this;
        }

        public HttpRequestBuilder header(String key, String value) {
            this.request.addHeader(key, value);
            return this;
        }

        public HttpRequestBuilder body(byte[] body) {
            this.request.setBody(body);
            return this;
        }

        public HttpRequest build() {
            return this.request;
        }

    }

}
